
import requests,json

class BaiduApi():
    def __init__(self):
        self.API_KEY = "8RtayBigIXUFX7IPss0gy49J"
        self.SECRET_KEY = "xFL9PW9uaKt4zoXBlMXPUl4fxPE3uhkd"


    def get_access_token(self):
        """
    使用 AK，SK 生成鉴权签名（Access Token）
    :return: access_token，或是None(如果错误)
    """
        url = "https://aip.baidubce.com/oauth/2.0/token"
        params = {"grant_type": "client_credentials", "client_id": self.API_KEY, "client_secret": self.SECRET_KEY}
        return str(requests.post(url, params=params).json().get("access_token"))

    #身份证验证
    def idcard(self,picurl):
        
        url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + self.get_access_token()
    
        # image 可以通过 get_file_content_as_base64("C:\fakepath\1.png",True) 方法获取
        payload='id_card_side=front&url=%s&detect_ps=false&detect_risk=false&detect_quality=false&detect_photo=false&detect_card=false&detect_direction=false'%(picurl)
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
        }
        
        response = requests.request("POST", url, headers=headers, data=payload)
        print(response.text)
        return response

    #文字识别
    def fontmessage(self,picurl):
        url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + self.get_access_token()
    
        payload='url=%s&detect_direction=false&paragraph=false&probability=false'%(picurl)
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
        }
        response = requests.request("POST", url, headers=headers, data=payload)
        data = json.loads(response.text)
        print('识别卡证信息成功........')
        print(data)
        name = data['words_result'][1]['words']
        code = data['words_result'][-1]['words']
        return {"name":name,'code':code}

bdapi = BaiduApi()
# data = bdapi.fontmessage("http%3A//fuguang0.oss-cn-beijing.aliyuncs.com/uploads%252Fimage-20241129185610739.png%3FOSSAccessKeyId%3DLTAI5t723w3A9pyJ92Y1gnvP%26Expires%3D1733148231%26Signature%3D%252Fcjzo3qTHpTVWyrKuezb1SMIlyA%253D")
# print(data)
# data数据:
# {'words_result': [{'words': '姓名'}, {'words': '蓝兔'}, {'words': '性别'}, {'words': '女'}, {'words': '民族汉'}, {'words': '出生'}, {'words': '2006年9月5日'}, {'words': '住址'}, {'words': '张家界玉蟾宫'}, {'words': '公民身份号码'}, {'words': '160370200609051128'}], 'words_result_num': 11, 'log_id': 1863578440141572742}
